﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using System.IO;
public partial class Admin_ProUpdate : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int pid = int.Parse(Request.QueryString["id"].ToString());
            Bind(pid);
            BindClass();
        }
    }
    private void Bind(int pid)
    {
        Products Addp = new Products();
        OleDbDataReader dr = Addp.GetProForPid(pid);
        if (dr.Read())
        {
            textpname.Text = dr["Pname"].ToString();
            TextTit.Text = dr["Ptitle"].ToString();
            TextKey.Text = dr["Pkey"].ToString();
            TextDes.Text = dr["Pjj"].ToString();
            TextUrl.Text = dr["HtmlUrl"].ToString();
            ncontent.Value = dr["Pconn"].ToString();
            TextMaoDian.Text = dr["maodian"].ToString();
            TextBox1.Text = dr["Psort"].ToString();
            int Ptypeid = int.Parse(dr["Ptypeid"].ToString());
            clsshow.Items.Add(new ListItem(WebSet.GetProPTypeForPTypeId(Ptypeid, "Pname"), Ptypeid.ToString()));
            Label1.Text = dr["Psimg"].ToString();
        }
        dr.Close();
        dr.Dispose();
 
    }
    private void BindClass()
    {
        Products GetP = new Products();
        OleDbDataReader dr = GetP.GetPtype();
        while (dr.Read())
        {
            clsshow.Items.Add(new ListItem(dr["Pname"].ToString(), dr["Ptypeid"].ToString()));
        }
        dr.Close();
        dr.Dispose();
    }


    protected void Button1_Click(object sender, EventArgs e)
    {

        Products Addp = new Products();
        if (TextUrl.Text.Trim() != "")
        {
            int Ptypeid = int.Parse(clsshow.SelectedValue);
            string Tit1 = textpname.Text;
            string Tit2 = TextTit.Text;
            string Key = TextKey.Text;
            string Des = TextDes.Text;
            string Url = TextUrl.Text;
            string Conn = ncontent.Value;
            string Maodian = TextMaoDian.Text;
            int Psort = int.Parse(TextBox1.Text);
            string photoName = Label1.Text;
            if (this.npicurl.PostedFile != null && this.npicurl.PostedFile.FileName != "")
            {
                UpLoadFile files = new UpLoadFile();
                photoName = files.UpLoadFileImg(this.npicurl.PostedFile, this.npicurl.PostedFile.FileName);
            }

            if (Keybool.Checked)
            {
                Conn = SetConnLink.FormatBody(Conn);
            }

            Addp.UPPPro(Ptypeid, Tit1, Tit2, Key, Des, Url, Conn, photoName, Psort, Maodian, int.Parse(Request.QueryString["id"].ToString()));
            //
            string Toplianjie = WebSet.GetSingleValue("顶部连接");
            string hongxingwangzhan = WebSet.GetSingleValue("红星网站导航");
            string ProList = WebSet.GetTopnProNoPic(14);
            string NewsList = WebSet.GetTopnNews(10, 18);
            string dianhua = WebSet.GetProPTypeForPTypeId(Ptypeid, "Pphone");
            string DibuLianjie = WebSet.GetSingleValue("底部版权");

            string ProType = "<a href=\"ProductList.aspx?PType=" + Ptypeid + "\">" + WebSet.GetProPTypeForPTypeId(Ptypeid, "Pname") + "</a>";
            Tit2 += WebSet.GetSingleValue("WebTitle").ToString();
            //生成页面
            try
            {
                StringBuilder strhtml = new StringBuilder();
                //创建StreamReader对象
                using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/") + "ProMoBan.html", Encoding.GetEncoding("gb2312")))
                {
                    String oneline;
                    //读取指定的HTML文件模板
                    while ((oneline = sr.ReadLine()) != null)
                    {
                        strhtml.Append(oneline);
                    }
                    sr.Close();
                }
                string FilePath = "";
                strhtml = strhtml.Replace("$title", Tit2);
                strhtml = strhtml.Replace("$keywords", Key);
                strhtml = strhtml.Replace("$description", Des);
                strhtml = strhtml.Replace("$ProductName", Tit1);
                strhtml = strhtml.Replace("$conn", Conn);
                strhtml = strhtml.Replace("$ProImg", photoName);
                strhtml = strhtml.Replace("$danqiandizhi", Url);
                strhtml = strhtml.Replace("$jianjie", Des);
                strhtml = strhtml.Replace("$Toplianjie", Toplianjie);
                strhtml = strhtml.Replace("$hongxingwangzhan", hongxingwangzhan);
                strhtml = strhtml.Replace("$ProList", ProList);
                strhtml = strhtml.Replace("$dianhua", dianhua);
                strhtml = strhtml.Replace("$DibuLianjie", DibuLianjie);
                strhtml = strhtml.Replace("$ProType", ProType);
                strhtml = strhtml.Replace("$maodian", Maodian);
                strhtml = strhtml.Replace("$NewsList", NewsList);

                FilePath = System.Web.HttpContext.Current.Server.MapPath("~/") + Url;
                //删除原有文件
                FileInfo file = new FileInfo(FilePath);
                if (file.Exists)
                {
                    file.Delete();
                }
                //创建文件信息对象--------------------------------------------
                FileInfo finfo = new FileInfo(FilePath);
                //以打开或者写入的形式创建文件流
                using (FileStream fs = finfo.OpenWrite())
                {
                    //根据上面创建的文件流创建写数据流
                    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
                    //把新的内容写到创建的HTML页面中
                    sw.WriteLine(strhtml);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception err)
            {
                //输出异常信息
                throw new Exception(err.Message, err);
            }
            Response.Write("<script>alert('修改完成！')</script>");
        }
        else
        {
            Response.Write("<script>alert('页面地址不能为空或重复')</script>");
        }
    }
}
